/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */

package cn.webrx;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeWriter;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: jse2303 - QrCodeDemo
 * <p>Powered by webrx On 2023-07-25 10:24:28
 * <p>描述：<p>
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class QrCodeDemo {
    public static void main(String[] args) {
        //解析读取二维码


        //解析图像
        try {
            BufferedImage i = ImageIO.read(new File("d:/ccc.jpg"));
            LuminanceSource source = new BufferedImageLuminanceSource(i);
            BinaryBitmap image = new BinaryBitmap(new HybridBinarizer(source));
            Result result = new MultiFormatReader().decode(image);
            String info = result.getText();
            System.out.println(info);
            if (info == null || "".equals(info)) {
                System.out.println("没有二维码信息");
            } else if (info.contains("weixin")) {  //qq mp
                System.out.println("有微信二维码，不允许");
            } else {
                System.out.println("有二维，可以使用");
            }
        } catch (Exception e) {
            System.out.println("图像中没有二维码");
        }

        System.exit(0);

        //支持中文
        Map<EncodeHintType, String> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.MARGIN, "1");
        //生成二维码
        int w = 220;
        int h = 220;
        String text = "http://img1.baidu.com/it/u=1828664006,303765003&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500";
        try {
            QRCodeWriter writer = new QRCodeWriter();
            BitMatrix encode = writer.encode(text, BarcodeFormat.QR_CODE, w, h, hints);
            BufferedImage img = MatrixToImageWriter.toBufferedImage(encode);
            ImageIO.write(img, "jpg", new File("qr1.jpg"));
        } catch (Exception e) {
        }
        //03 MatrixToImageWriter.toBufferedImage(encode); 将BitMatrix 工具toBufferedImage() 返回BufferedImage
    }
}
